const app = require('./app/app.js');
const express = require('express');
const application = express();

//定义解析md请求地址
application.get('/docs/:file',function(request,response) {
        if(request.params.file){
            const msg = app.check(request.params.file);
            msg.flag=='0'?response.sendFile(msg.result):response.send(msg.result);
        }else {
            response.setStatus(404);
            response.send('Sorry! The Resource you asked for doesn\'t exist!')
        }
    
});
/**
 * 设置全局中间件
 */
application.use(function(req, res, next){
    if(req.url.indexOf('/favicon.ico')===0) {
    }else if(req.url.indexOf('/docs/')===-1||req.url.endsWith('/docs/')){
        res.send('Sorry! There is nothing matches your request!')
    }
    next();
});
/**
 * 设置静态资源访问和ico访问路径中间件
 */
application.use('/docs/assets', express.static(app.path.resolve(__dirname,'./node_modules/markdown-styles/layouts/github/assets'))); 
application.use('/favicon.ico',express.static(app.path.resolve(__dirname,'./favicon.ico')))
/**
 * 设置全局异常中间件
 */
application.use(function(err, req, res, next) {
    console.error(err.stack);
    res.status(500).send('Sorry！');
});

/**
 * 启动服务
 */
application.listen(4000, function() {
    const host = this.address().address;
    const port = this.address().port;
    console.log('server running at ', host, port);
});

